<form class="layui-form seller-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">操作时间：</label>

            <div class="layui-input-inline">
                <input type="text" name="date" id="date" placeholder="开始时间 到 结束时间" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="*"><i class="iconfont icon-chaxun"></i>筛选</button>
                <button id="exportBtn" type="button" class="layui-btn layui-btn-sm layui-btn-disabled"  lay-submit lay-filter="export-operation_log" disabled><i class="iconfont icon-clouddownload" style="font-size: 20px !important;"></i>导出</button>
            </div>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="operationTable" lay-filter="operation"></table>
</div>
<div id="exportOperationLog" style="display: none;">
    <form class="layui-form export-form"  action="" >
        <div class="layui-form-item">
            <div class="layui-margin-10">
                <blockquote class="layui-elem-quote layui-text">
                    请先选中或筛选要导出的数据
                </blockquote>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">任务名称：</label>
                <input type="text" name="taskname" lay-verify="title" style="width:200px;" placeholder="请输入任务名称" autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</div>

<script>
    var pid = 1;
    layui.use(['form', 'layedit', 'laydate', 'table'], function () {

        var layer = layui.layer,
                $ = layui.jquery,
                form = layui.form,
                laydate = layui.laydate;
        //时间插件
        laydate.render({
            elem: '#date',
            range: '到',
            format: 'yyyy-MM-dd'
        });

        var operationTables = layui.table.render({
            elem: '#operationTable',
            height: 'full-115',
            cellMinWidth: '80',
            page: true,
            limit: '20',
            url: "{:url('OperationLog/index')}?_ajax=1",
            id: 'operationTable',
            cols: [[
                {type: 'numbers'},
                {field: 'username', title: '操作员', width:120},
                {field: 'ctime', title: '操作时间', width:160},
                {field: 'desc', title: '操作描述', width:200},
                {field: 'content', title: '操作内容', width:500},
                {field: 'ip', title: '操作IP', width:140}
            ]],
            done: function(res, curr, count){
                // 判断加载结果数量是否大于0
                if(count>0){
                    $("#exportBtn").removeClass("layui-btn-disabled");
                    $('#exportBtn').removeAttr("disabled");
                } else {
                    $('#exportBtn').addClass("layui-btn-disabled");
                    $('#exportBtn').attr("disabled", true);
                }
            }
        });

        //筛选条件
        form.on('submit(*)', function(data){
            operationTables.reload({
                where: data.field,
                page: {curr: 1}
            });
            return false;
        });
        //操作日志导出
        layui.form.on('submit(export-operation_log)', function(data){

            layer.open({
                type: 1,
                title:'操作日志导出',
                area: ['400px', '248px'], //宽高
                btn:['确定','取消'],
                content: $("#exportOperationLog").html(),
                success: function () {
                    $(':focus').blur();
                    this.enterEsc = function(event){
                        if(event.keyCode === 13){
                            if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                            $(".layui-layer-btn0").addClass("layui-btn-disabled");

                            //判断是否有选中
                            var checkStatus = layui.table.checkStatus('operationTable');
                            var checkData = checkStatus.data;
                            var length = checkStatus.data.length;
                            var ids = [];
                            if(length){
                                $.each(checkData,function (i,obj) {
                                    ids.push(obj.id);
                                });
                            }

                            //判断是否有选中
                            var filter = $(".seller-form").serialize();
                            filter+='&ids='+ids;
                            $(".export-form:last").append("<input type='hidden' name='filter' value='"+filter+"'>");
                            var data = $(".export-form:last").serializeArray();
                            
                            if($.trim(data[0]['value']) == '') {
                                layer.msg('请填写任务名称！', { time: 1300 });
                                $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                return false;
                            }
                            
                            data.push({'name':'model','value':'OperationLog'});

                            JsPost("{:url('Ietask/export')}",data,function(res){
                                    layer.msg(res.msg, {time:1500}, function(){
                                        if(res.status){
                                            operationTables.reload({
                                                where: data.field,
                                                page: {curr: 1}
                                            });
                                            layer.closeAll();
                                            
                                            //执行跳转
                                            var topLayui = parent === self ? layui : top.layui;
                                            topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                        } else { 
                                            $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                        }
                                    });
                                }
                            );
                        }
                            return false; //阻止系统默认回车事件
                        }
                    };
                    $(document).on('keydown', this.enterEsc);	//监听键盘事件，关闭层
                },
                yes:function () {
                    if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                        $(".layui-layer-btn0").addClass("layui-btn-disabled");

                        //判断是否有选中
                        var checkStatus = layui.table.checkStatus('operationTable');
                        var checkData = checkStatus.data;
                        var length = checkStatus.data.length;
                        var ids = [];
                        if(length){
                            $.each(checkData,function (i,obj) {
                                ids.push(obj.id);
                            });
                        }

                        //判断是否有选中
                        var filter = $(".seller-form").serialize();
                        filter+='&ids='+ids;
                        $(".export-form:last").append("<input type='hidden' name='filter' value='"+filter+"'>");
                        var data = $(".export-form:last").serializeArray();

                        if($.trim(data[0]['value']) == '') {
                            layer.msg('请填写任务名称！', { time: 1300 });
                            $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                            return false;
                        }

                        data.push({'name':'model','value':'OperationLog'});

                        JsPost("{:url('Ietask/export')}",data,function(res){
                                layer.msg(res.msg, {time:1500}, function(){
                                    if(res.status){
                                        operationTables.reload({
                                            where: data.field,
                                            page: {curr: 1}
                                        });
                                        layer.closeAll();
                                        //执行跳转
                                        var topLayui = parent === self ? layui : top.layui;
                                        topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                    } else { 
                                        $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    }
                                });
                            }
                        );
                    }
                },btn2:function () {
                    layer.closeAll();
                }, end: function(){
                    $(document).off('keydown', this.enterEsc);	        //解除键盘关闭事件
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>
